package com.lixl.threadpool;

import java.util.Random;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * <a href="http://www.cnblogs.com/zhanjindong/p/java-concurrent-package-ThreadPoolExecutor.html">ThreadPoolExecutor源码解析</a>
 * @author Administrator
 *
 */
public class ExecutorServiceDemo {

	public static void main(String[] args) throws InterruptedException, ExecutionException {
		ExecutorService executor = Executors.newFixedThreadPool(20);
		//Executors.newCachedThreadPool();
		//Executors.newScheduledThreadPool(10);
		//ExecutorService executor = Executors.newSingleThreadExecutor();
		/*executor.execute(new Runnable() {
			@Override
			public void run() {
				System.out.println("i");
			}
		});*/
		for (int i = 0; i < 50; i++) {
			String result = executor.submit(new Callable<String>() {

				@Override
				public String call() throws Exception {
					int result =(int)( new Random().nextDouble() * 20);
					String name=Thread.currentThread().getName();
					return name+"-"+result;
				}
			}).get();
			System.out.println(i+"\t"+result);
		}
		executor.shutdown();
	}

}
